package com.sks.web.action.house;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;

import com.sks.bean.QueryResult;
import com.sks.bean.pojo.Area;
import com.sks.bean.pojo.Building;
import com.sks.bean.pojo.House;
import com.sks.bean.pojo.HouseType;
import com.sks.bean.pojo.PropertyType;
import com.sks.service.house.AreaService;
import com.sks.service.house.HouseService;
import com.sks.service.house.HouseTypeService;
import com.sks.service.house.MapService;
import com.sks.service.house.PropertyTypeService;
import com.sks.web.action.BasicAction;
import com.sks.web.formbean.house.MapInfoForm;


@Controller("/sks/map")

public class MapAction extends BasicAction {
	@Resource(name = "areaServiceBean") private AreaService areaService;
	@Resource(name = "houseTypeServiceBean") private HouseTypeService houseTypeService;
	@Resource(name = "propertyTypeServiceBean") private PropertyTypeService ptService;
	
	public static String BUILDING_STATUS_PUBLISHED = "published";
	
	@Resource(name="mapServiceBean")
	private MapService mapService;
	
	@Resource(name="houseServiceBean")
	private HouseService houseService;
	//TODO 地图的小区，需要小区是发布状态，且小区里面有房源，房源状态为发布
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Area> areas = areaService.findTopArea(true);
		List<HouseType> hts = houseTypeService.getScrollData().getResultlist();
		List<PropertyType> ppts = ptService.getScrollData(-1, -1).getResultlist();
		MapInfoForm mapInfoForm = (MapInfoForm)form;
		
		StringBuffer jpql = new StringBuffer();
		
		List<Object> params = new ArrayList<Object>();
		
		params.add(BUILDING_STATUS_PUBLISHED);
		jpql.append( " from Building o join o.houses h where  o.status = ?" + params.size());
		params.add(House.HOUSE_STATUS_PUBLISHED);
		jpql.append( " and h.houseStatus =?" + params.size());
		
		
		/* 默认所有条件为空*/
	//	jpql.append("from Building");
	 	QueryResult<Building> m = mapService.getScrollDataByhql(-1, -1, jpql.toString(),
	 			 params.toArray());
		List<Building> rs = m.getResultlist();

		StringWriter sw = new StringWriter();
		JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
		ObjectMapper objectMapper = new ObjectMapper();
 		objectMapper.writeValue(gen, rs);
		
		request.setAttribute("resultSet",sw.toString());
		request.setAttribute("tops", areas);
		request.setAttribute("hts", hts);
		request.setAttribute("ppts", ppts);
		
		objectMapper=null;
		gen=null;
		sw=null;
		return mapping.findForward(LIST);
	
	}
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MapInfoForm mapInfoForm = (MapInfoForm)form;
		
		
		StringBuffer jpql = new StringBuffer("1=1 ");
		List<Object> params = new ArrayList<Object>();
		
	//	System.out.println("sw :" + mapInfoForm.getSw());
	//	System.out.println("ne :" + mapInfoForm.getNe());
		String sw = mapInfoForm.getSw();
		String ne = mapInfoForm.getNe();
	
		Double swLng = Double.valueOf(sw.substring(0,sw.indexOf("|")));
		Double swLat = Double.valueOf(sw.substring(sw.indexOf("|")+1,sw.length()));
		Double neLng = Double.valueOf(ne.substring(0,ne.indexOf("|")));
		Double neLat = Double.valueOf(ne.substring(ne.indexOf("|")+1,ne.length()));
		
		//左下角边界
	 	params.add(swLng);
	 	jpql.append(" and o.lng >?"+params.size());
	 	params.add(swLat);
	 	jpql.append(" and o.lat >?"+params.size());
	 	params.add(neLng);
	 	//右上角边界
	 	jpql.append(" and o.lat <?"+params.size());
	 	params.add(neLat);
	 	jpql.append(" and o.lat <?"+params.size());
	 	
	 	params.add(BUILDING_STATUS_PUBLISHED);
		jpql.append(" and o.status = ?" + params.size());
		
		jpql.append(" and o.houses is not empty ");
	 	QueryResult<Building> m = mapService.getScrollData(-1, -1, jpql.toString(),
	 			 params.toArray());
	 	List<Building> ls = new ArrayList<Building>();
		List<Building> rs = m.getResultlist();
		for(Building b :rs){
			Set<House> houses = b.getHouses();
			for(House h :houses){
				//房源状态为发布
				if(House.HOUSE_STATUS_PUBLISHED.equals(h.getHouseStatus())){
					ls.add(b);
				}
			}
		}
	 	StringWriter swt = new StringWriter();
	 	JsonGenerator gen = new JsonFactory().createJsonGenerator(swt);
		ObjectMapper objectMapper = new ObjectMapper();
 		objectMapper.writeValue(gen, ls);
		
		//request.setAttribute("resultSet","aaaa");
 		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(swt.toString());

		ls=null;
		objectMapper=null;
		gen=null;
		swt=null;
		
		return null;
	
	}
	

	
}
